<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8" />
        <title>账户充值-{eyou:global name='web_name' /}</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
        <link href="{eyou:global name='web_cmspath'/}/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        {eyou:static file="users/skin/css/basic.css" /}
        {eyou:static file="users/skin/css/eyoucms.css" /}
        {eyou:static file="users/skin/css/element/index.css" /}
        {eyou:static file="users/skin/css/e-user.css" /}
        {eyou:include file="users/skin/css/diy_css.htm" /}
        {eyou:static file="/public/static/common/js/jquery.min.js"/}
        {eyou:static file="/public/plugins/layer-v3.1.0/layer.js"/}
        {eyou:static file="users/skin/js/global.js"/}
        {eyou:static file="/public/static/common/js/mobile_global.js"/}
    </head>

    <body>
        <div class="ey-header-status">
            <div class="header-status-l"><a href="javascript:history.go(-1);"><i class="el-icon-arrow-left"></i></a></div>
            <div class="header-status-m">账户充值</div> 
            <div class="header-status-r"><a href="{eyou:url link='user/Pay/pay_consumer_details' /}" class="addr_z ml10">明细</a></div>
        </div>
        <div class="h50"></div>

        <div class="ey-container">
            <div class="ey-con">
                <form id="postForm">
                    <div class="ey-row">
                        <div class="balance">
                            <i class="m-pay-def-pay"></i>
                            <p class="title">账户余额（元）</p>
                            <p class="money">{$users.users_money}</p>
                        </div>
                    </div>
                    <div class="ey-row">
                        <div class="recharge-box">
                            <div class="recharge-box-title">充值金额</div>
                            <div class="recharge-box-con">
                                <i>￥</i>
                                <input type="text" class="el-input__inner" onkeyup="this.value=this.value.replace(/[^0-9\.]/g,'');" onafterpaste="this.value=this.value.replace(/[^0-9\.]/g,'');" name="money" value="{$money}" placeholder="输入充值金额">
                            </div>
                        </div>
                    </div>
                    <div class="ey-row mt10 pt0 pb0 ">
                        <div class="item-from-row recharge-method bgfff border-b-no" id="div_zhifufangshi" style="display: none;">
                            <div class="from-row-r wb100">
                                <!-- 支付API列表，套板请务必将整块代码一起复制，包含隐藏域 -->
                                {eyou:sppayapilist id='vo'}
                                {eyou:notempty name="$vo.pay_id"}
                                <label class="radio-label pt10 pb10 radio-label-flex pl0 mb0 e-label {eq name='$key' value='0'} active {/eq}" data-type="zxzf_{$vo.pay_mark}">
                                    <span><img class="pay_img" src="{$vo.pay_img}" alt="{$vo.pay_name}">{$vo.pay_name} </span>
                                    <input class="radio" type="radio" name='pay_id' value="{$vo.pay_id}" {eq name='$key' value='0'} checked="checked" {/eq}>
                                    <span class="check-mark ri0"></span>
                                    <input type="hidden" name='pay_mark[{$vo.pay_id}]' value="{$vo.pay_mark}">
                                </label>
                                {/eyou:notempty}
                                {$vo.hidden}
                                {/eyou:sppayapilist}
                                <input type="hidden" id="PayID">
                                <input type="hidden" id="PayMark">
                                <script type="text/javascript">
                                    $(function(){
                                        if ($('#div_zhifufangshi').find('.radio-label').length > 0) $('#div_zhifufangshi').show();
                                    });
                                </script>
                                <!-- END -->
                            </div>
                        </div>
                        <div class="public-btn">
                            <input type="hidden" name="payment_type" >
                            <input type="hidden" name="unified_number" value="{$unified_number}">
                            <a href="JavaScript:void(0);" id="pay_btn" onclick="pay_money();">立即支付</a>
                        </div>
                    </div>
                </form>
            </div>
            <input type="hidden" id="unified_id">
            <input type="hidden" id="unified_number">
            <input type="hidden" id="transaction_type">
        </div>
        {eyou:static file="users/skin/js/pay_account_recharge.js" /}

        <script type="text/javascript">
            $(function(){
                $('input[name=money]').focus();
                // 默认支付方式
                var default_payment_type = $('#div_zhifufangshi .radio-label').attr('data-type');
                $('input[name=payment_type]').val(default_payment_type);
                // 支付类型选择
                $('#div_zhifufangshi .radio-label').click(function(){
                    $('input[name=payment_type]').val($(this).data('type'));
                });

                wx.miniProgram.getEnv( function(res) {
                    if(res.miniprogram) {
                        // 小程序
                        var i = 0;
                        i = setInterval('AppletsPay()', 1000);
                    }
                });
            });

            // 小程序后查询
            function AppletsPay(){
                var unified_id       = $('#unified_id').val();
                var unified_number   = $('#unified_number').val();
                var transaction_type = $('#transaction_type').val();
                if (unified_id && unified_number && transaction_type) {
                    $.ajax({
                        url: "{eyou:url link='user/Pay/ajax_applets_pay'/}",
                        data: {unified_id:unified_id, unified_number:unified_number, transaction_type:transaction_type},
                        type:'post',
                        dataType:'json',
                        success:function(res){
                            if (1 == res.code) {
                                window.location.href = res.url;
                            }
                        }
                    });    
                }
            }

            function pay_money() {
                var money = $('input[name=money]');
                if($.trim(money.val()) == ''){
                    showLayerMsg('充值金额不能为空');
                    money.focus();
                    return false;
                } else if (isNaN($.trim(money.val()))) {
                    showLayerMsg('请输入正确的充值金额');
                    money.focus();
                    return false;
                }
                if ($('input[name=payment_type]').val() == '') {
                    showLayerMsg('请选择支付方式');
                    return false;
                }

                showLayerLoad();
                $.ajax({
                    async: false,
                    url : "{:url('user/Pay/pay_account_recharge', ['version'=>'v2', '_ajax'=>1])}",
                    data: $('#postForm').serialize(),
                    type: 'post',
                    dataType: 'json',
                    success: function(res) {
                        layer.closeAll();
                        if (res.code == 1) {
                            if (0 == res.data.is_gourl) {
                                $('#unified_id').val(res.data.unified_id);
                                $('#unified_number').val(res.data.unified_number);
                                $('#transaction_type').val(res.data.transaction_type);
                                WeChatInternal(res.data);
                            } else {
                                SelectPayMethod_2(res.data.pay_id, res.data.pay_mark, res.data.unified_id, res.data.unified_number, res.data.transaction_type);
                            }
                        }else{
                            layer.closeAll();
                            showErrorAlert(res.msg);
                        }
                    },
                    error : function(e) {
                        layer.closeAll();
                        showErrorAlert(e.responseText);
                    }
                });
            }

            // 微信内部中进行支付
            function WeChatInternal(wechatdata)
            {   
                wx.miniProgram.getEnv( function(res) {
                    if(res.miniprogram) {
                        // 小程序
                        wx.miniProgram.navigateTo({
                            url: '/pages/pay/pay?unified_id='+ wechatdata['unified_id'] +'&unified_number=' + wechatdata['unified_number'] + '&type=' + wechatdata['transaction_type']
                        });
                    } else {
                        // 微信端
                        $.ajax({
                            url: "{eyou:url link='user/Pay/wechat_pay'/}",
                            data: wechatdata,
                            type:'post',
                            dataType:'json',
                            success:function(res){
                                if (1 == res.code) {
                                    callpay(res.msg);
                                }else{
                                    showErrorAlert(res.msg, 0);
                                }
                            }
                        });
                    }
                });
            }

            //调用微信JS api 支付
            function jsApiCall(data)
            {
                WeixinJSBridge.invoke(
                    'getBrandWCPayRequest',data,
                    function(res){
                        if(res.err_msg == "get_brand_wcpay_request:ok"){  
                            layer.msg('微信支付完成！', {time: 1000}, function(){
                                pay_deal_with();
                            });
                        }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                            layer.msg('用户取消支付！', {time: 1000}, function(){
                                window.location.reload();
                            });
                        }else{
                            showErrorAlert('支付失败！', 0);
                        }  
                    }
                );
            }
            
            // 微信内部支付时，先进行数据判断
            function callpay(data)
            {
                if (typeof WeixinJSBridge == "undefined"){
                    if( document.addEventListener ){
                        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                    }else if (document.attachEvent){
                        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
                        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                    }
                }else{
                    jsApiCall(data);
                }
            }

            function pay_deal_with(){
                var unified_number   = $('#unified_number').val();
                var transaction_type = $('#transaction_type').val();
                $.ajax({
                    url: "{eyou:url link='user/Pay/pay_deal_with'/}",
                    data: {unified_number:unified_number,transaction_type:transaction_type},
                    type:'post',
                    dataType:'json',
                    success:function(res){
                        if (1 == res.data.status) {
                            window.location.href = res.url;
                        }
                    }
                });
            }
        </script>
    </body>
</html>